package com.mall.manager.authority.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.mall.manager.authority.service.PartService;
import com.mall.untils.Page;
import com.mall.untils.StringUtil;

/**
 * 角色信息列表
 * @author liugb
 * @date 2016 7 7
 */
@Controller
@RequestMapping("/manager")
public class PartController {
	Logger logger = LoggerFactory.getLogger(PartController.class);
	@Resource PartService partService;
	  /**
	   * 角色信息列表
	   * @param request
	   * @param response
	   * @param modelMap
	   * @return
	   */
	  @SuppressWarnings("unchecked")
	@RequestMapping(value = "/manager_tpart_list")
	    public String manager_tpart_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Page page=partService.manager_tpart_list(request);
	    	Map<String,Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	String name = MapUtils.getString(paraMap, "name");
	    	if(!StringUtils.isEmpty(name)){
	    		modelMap.addAttribute("name",StringUtil.getByteToString(name));
	    	}
	     	if(null!=page){
		     	modelMap.addAttribute("manager_tpart_list", (List<Map<String,Object>>)page.getResult());
		     	int totalpagecount = page.getTotalPageCount();
		     	modelMap.addAttribute("totalpagecount", totalpagecount);
		     	int currentpageno = page.getCurrentPageNo();
		     	modelMap.addAttribute("currentpageno", currentpageno);
		     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
						.getCurrentPageNo() - 1;
		     	modelMap.addAttribute("previouspage", previouspage);
		     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
						.getTotalPageCount() : page.getCurrentPageNo() + 1;
				modelMap.addAttribute("nextpage", nextpage);
				List<Integer> showNumList = page.getShowNumList();
				modelMap.addAttribute("pagenumlist", showNumList);
	     	}
	    	return "roles/role_list";
	    }
	  /**
	   * 编辑页面跳转
	   * @param request
	   * @param response
	   * @param modelMap
	   * @return
	   */
	  @RequestMapping(value = "/tpart_edit")
	    public String tusers_edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Map<String,Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	int id = MapUtils.getIntValue(paraMap, "id");
	    	if(id!=0){
	    		Map<String, Object> map = partService.findById(id);
	    		modelMap.addAttribute("id", id);
	    		modelMap.addAttribute("name", map.get("name"));
	    		modelMap.addAttribute("remarks", map.get("remarks"));
	    	}
	    	return "roles/role_add";
	    } 
	    /**
	     * 删除角色信息
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
	    @RequestMapping(value = "/manager_del_Tpart",method=RequestMethod.GET)    
	    public ModelAndView manager_del_Tpart(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	        partService.manager_del_tpart(paraMap);
	    	return new ModelAndView("redirect:/manager/manager_tpart_list?currentpageno=1");
	    }
	    
	    /**
	     * 更改角色信息
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
	    @RequestMapping(value = "/manager_update_Tpart")    
	    @ResponseBody
	    public String manager_update_Tpart(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	int update=partService.manager_update_tpart(paraMap);
	    	return String.valueOf(update);
	    }	  
	    /**
	     * 添加角色方法
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
	    @RequestMapping(value = "/manager_insert_Tpart")    
	    @ResponseBody
	    public String manager_insert_Tpart(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
	    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	int update=partService.manager_insert_tpart(paraMap);
	    	return String.valueOf(update);
	    }	
	    
	    /**
	     * 编辑页面
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
	    @RequestMapping(value = "/manager_save_Tpart",method=RequestMethod.POST)    
	    public ModelAndView save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
	    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    	String id = String.valueOf(paraMap.get("id"));
	    	if(StringUtils.isEmpty(id)){
	    		partService.manager_insert_tpart(paraMap);
	    	}else{
	    		partService.manager_update_tpart(paraMap);
	    	}
	    	return new ModelAndView("redirect:/manager/manager_tpart_list?currentpageno=1");
	    }
}
